//
//  CompanyDao.m
//  Contacts
//
//  Created by 武 娇 on 14-7-31.
//  Copyright (c) 2014年 李 雷川. All rights reserved.
//

#import "CompanyDao.h"

@implementation CompanyDao
-(BOOL)insertCompany:(Company *)company{
    BOOL success = YES;
	[db executeUpdate:[self SQL:@"INSERT INTO %@ (id,name,member_count) VALUES(?,?,?)" inTable:Company_TABLE],
     company.ID,
     company.name,
     @(company.memberCount)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}
-(BOOL)updateCompany:(Company *)company{
    BOOL success = YES;
    [db executeUpdate:[self SQL:@"UPDATE %@ SET name =?,member_count=? WHERE id = ?" inTable:Company_TABLE],
     company.name,
     company.ID,
     @(company.memberCount)
     ];
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
		success = NO;
    }
	return success;
}
-(Company *)getCompanyWithID:(NSString *)companyID{
    Company *company = nil;
    FMResultSet *rs =[db executeQuery:[self SQL:@"select * from %@ where id = ? " inTable:Company_TABLE],companyID];
    while ([rs next]) {
        company = [self analysisCompanyWithRS:rs];
    }
    if ([db hadError]) {
		NSLog(@"Err %d: %@", [db lastErrorCode], [db lastErrorMessage]);
	}
    return company;

}
-(Company *)analysisCompanyWithRS:(FMResultSet *)rs{
    Company *company = [[Company alloc]init];
    company.ID = [rs stringForColumn:@"id"];
    company.name =[rs stringForColumn:@"name"];
    company.memberCount = [rs intForColumn:@"member_count"];
    return company;
}
@end
